package com.omazan.nativeClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.omazan.db.HandleCustomers;
import com.omazan.db.HandleProductTypes;
import com.omazan.db.HandleProducts;
import com.omazan.db.HandleUsers;
import com.omazan.db.models.Customer;
import com.omazan.db.models.Product;
import com.omazan.db.models.ProductType;
import com.omazan.db.models.User;
import com.omazan.utils.OmazanException;
 

public class ProdTypeOperations {
	
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	
	public void addProdType() throws OmazanException, IOException{
		
			
			ProductType pt1 = new ProductType();
			
			System.out.println("Enter Product Type Name:");
			pt1.setProductType(input.readLine());
			HandleProductTypes hpt1 = new HandleProductTypes();
			hpt1.insertProductType(pt1);
	}
	public void delProdType() throws OmazanException, IOException{
		
		
		HandleProductTypes hpt1 = new HandleProductTypes();
		List<ProductType> listofProductTypes = hpt1
				.readAllProductTypesFromDB();
		for (ProductType eachProductType : hpt1.readAllProductTypesFromDB()) {
			System.out.println(eachProductType.getProductTypeId() + "|"
					+ eachProductType.getProductType());
		}

		System.out.println("Choose Product Type ID to delete:");
		int ptIdToDel = Integer.parseInt(input.readLine());

		//int prodIdDel = hpt1.readProductTypeFromDBUsingId(ptIdToDel)
				//.getProductTypeId();
		String delpt = hpt1.readProductTypeFromDBUsingId(ptIdToDel)
				.getProductType();
		System.out
				.println("Are you sure you want to delete this Product Type? Associated Products will be deleted.(y/n)");
		char choice=(char)input.read();
		if(choice=='y'||choice=='Y')
		{
			HandleProducts x=new HandleProducts();
			
			for (Product eachProduct : x.readAllProductsFromDB())
			{
				
			int del=eachProduct.getProductId();
			if(ptIdToDel==eachProduct.getProductType().getProductTypeId())
			{x.deleteUsingId(del);			}
				
			}
			
		hpt1.deleteUsingId(ptIdToDel);
				
		System.out.println("Product Type:" + delpt
				+ " Deleted from Product Database. Associated Products Deleted.");
		}
		else
		{
			System.out.println("Delete Aborted.");
		}
		
		
}

	public void dispProdType() throws OmazanException, IOException {
	
		HandleProductTypes hpt1 = new HandleProductTypes();
		
		List<ProductType> listofProductTypes = hpt1.readAllProductTypesFromDB();

		for (ProductType eachProductType : listofProductTypes) {
			System.out.println(eachProductType.getProductTypeId() + " | "
					+ eachProductType.getProductType());

		}
		
	}
}
